package com.dk.ar.dk_ar_vision_svn.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.http.HttpConnection;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.dk.ar.dk_ar_vision_svn.R;
import com.dk.ar.dk_ar_vision_svn.model.Geo;
import com.dk.ar.dk_ar_vision_svn.model.ResponseGeo;
import com.dk.ar.dk_ar_vision_svn.model.ResponseType;
import com.dk.ar.dk_ar_vision_svn.model.Type;
import com.dk.ar.dk_ar_vision_svn.util.GsonRequest;
import com.dk.ar.dk_ar_vision_svn.util.RestClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.gson.Gson;

public class DataProvider extends Thread{
	private ArrayList<Geo> geoList;
	private ArrayList<Geo> buddyList;
	private ArrayList<Marker> makerBuddyList;
	private ArrayList<Marker> makerGeoList;
	private ArrayList<Type> typeList;
	private GoogleMap gmap1;
	private GoogleMap gmap2;
	private int type_filter;
	public DataProvider() {
		geoList = new ArrayList<Geo>();
		buddyList = new ArrayList<Geo>();
		makerGeoList = new ArrayList<Marker>();
		makerBuddyList = new ArrayList<Marker>();
	}
	
	public ArrayList<Geo> getGeoList() {
		return geoList;
	}

	public ArrayList<Type> getTypeList() {
		return typeList;
	}

	public void setGoogleMap1(GoogleMap maps) {
		this.gmap1 = maps;
	}

	public void setGoogleMap2(GoogleMap maps) {
		this.gmap2 = maps;
	}
	public void loadGeoMaker(){
		//Log.i("DATA_PROVIER", ""+geoList);
		Iterator<Marker> iterM = this.makerGeoList.iterator();
		while(iterM.hasNext()) iterM.next().remove();
		this.makerGeoList.clear();
		if(geoList == null) return;
		Iterator<Geo> iter = geoList.iterator();
		while(iter.hasNext()){
			Geo geo = iter.next();
			if(type_filter != 0 && geo.getType() != type_filter) continue;
			Log.i("DATA_PROVIER", "" + geo.getName() + " "+geo.getLat()+" "+geo.getLng());
			LatLng loc = new LatLng(geo.getLat(), geo.getLng());
			MarkerOptions markerOption = new MarkerOptions()
										.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin))
										.position(loc)
										.title(geo.getName());

			Marker mark1 = this.gmap1.addMarker(markerOption);
			Marker mark2 = this.gmap2.addMarker(markerOption);
			this.makerGeoList.add(mark1);
			this.makerGeoList.add(mark2);
		}
		
	}
	public void loadBuddyMaker(){
		//Log.i("DATA_PROVIER", ""+buddyList);
		Iterator<Marker> iterM = this.makerBuddyList.iterator();
		while(iterM.hasNext()) iterM.next().remove();
		this.makerBuddyList.clear();
		if(buddyList == null) return;
		Iterator<Geo> iter = this.buddyList.iterator();
		while(iter.hasNext()){
			Geo geo = iter.next();
			//Log.i("DATA_PROVIER", "Buddy_" + geo.getName() + " "+geo.getLat()+" "+geo.getLng());
			LatLng loc = new LatLng(geo.getLat(), geo.getLng());
			MarkerOptions markerOption = new MarkerOptions()
										.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin2))
										.position(loc)
										.title(geo.getName());
			 
			Marker mark1 = this.gmap1.addMarker(markerOption);
			Marker mark2 = this.gmap2.addMarker(markerOption);
			this.makerBuddyList.add(mark1);
			this.makerBuddyList.add(mark2);
			
		}
		
	}
	public void updateGeoList(){
		
	}
	public void updateBuddyList(){
		
	}
	
	//geo_list
	//http://dk.r4gate.kr.pe/_include/app/appComponent.php?mode=geo_list
	
	//buddy_list
	//http://dk.r4gate.kr.pe/_include/app/appComponent.php?mode=buddy_list&user=1
	
	//push_geo
	//http://dk.r4gate.kr.pe/_include/app/appComponent.php?mode=push_geo&user=1&lat=37.4506299&lng=126.6578239
	
	public void geoDownload(int type){
		type_filter = type;
		String url = RestClient.client().fileToUrl();
		Uri.Builder builder = Uri.parse(url).buildUpon();
		builder.appendQueryParameter("mode", "geo_list");
		builder.appendQueryParameter("type", ""+type_filter);
		GsonRequest<ResponseGeo> geoGsonRequest = new GsonRequest<ResponseGeo>(Request.Method.GET, builder.toString(), ResponseGeo.class, null, successGeoListener, errorListener);
		RestClient.client().getRequestQueue().add(geoGsonRequest);
	}
	
	public void buddyDownload(){
		String url = RestClient.client().fileToUrl();
		Uri.Builder builder = Uri.parse(url).buildUpon();
		builder.appendQueryParameter("mode", "buddy_list");
		builder.appendQueryParameter("user", "1");
		
		GsonRequest<ResponseGeo> buddyGsonRequest = new GsonRequest<ResponseGeo>(Request.Method.GET, builder.toString(), ResponseGeo.class, null, successBuddyListener, errorListener);
		RestClient.client().getRequestQueue().add(buddyGsonRequest);
	}
	
	public void typeDownload(){
		String url = RestClient.client().fileToUrl();
		Uri.Builder builder = Uri.parse(url).buildUpon();
		builder.appendQueryParameter("mode", "type_list");
		
		GsonRequest<ResponseType> typeGsonRequest = new GsonRequest<ResponseType>(Request.Method.GET, builder.toString(), ResponseType.class, null, successTypeListener, errorListener);
		RestClient.client().getRequestQueue().add(typeGsonRequest);
	}
	
	private Listener<ResponseGeo> successGeoListener = new Listener<ResponseGeo>(){
		@Override
		public void onResponse(ResponseGeo response)
		{
			if(response == null || response.getData()==null) return;
			geoList = response.getData();
			Log.i("DATA_PROVIER", "" + response.getData().size());
			loadGeoMaker();
		}
	}; 
	private Listener<ResponseGeo> successBuddyListener = new Listener<ResponseGeo>(){
		@Override
		public void onResponse(ResponseGeo response)
		{
			if(response == null || response.getData()==null) return;
			buddyList = response.getData();
			Log.i("DATA_PROVIER", "" + response.getData().size());
			loadBuddyMaker();
		}
	}; 
	private Listener<ResponseType> successTypeListener = new Listener<ResponseType>(){
		@Override
		public void onResponse(ResponseType response)
		{
			if(response == null || response.getData()==null) return;
			typeList = response.getData();
			Log.i("DATA_PROVIER", "" + response.getData().size());
			loadBuddyMaker();
		}
	}; 
	
	private ErrorListener errorListener =  new ErrorListener()
		{
			@Override
			public void onErrorResponse(VolleyError error)
			{
				Log.i("DATA_PROVIER", ""+error);
			}
		};
	
	public void run() {
		while(true){
			try {
				Thread.sleep(3000);
				buddyDownload();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};
}
